<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item label="名称:">
        <el-input
          v-model="dataForm.name"
          placeholder="名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button
          v-if="isAuth('exter:app:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <!--   <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->

      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        width="150"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        width="70"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" size="small" type="success"
            >可用</el-tag
          >
          <el-tag v-else size="small" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="说明"
      >
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            class="update-button"
            size="small"
            v-if="isAuth('exter:app:update')"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="isAuth('exter:app:update')"
            @click="akUpdateHandle(scope.row.id)"
            >密钥</el-button
          >
          <el-button
            type="text"
            class="del-button"
            size="small"
            v-if="isAuth('exter:app:delete')"
            @click="deleteHandle(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
    <Ak v-if="akVisible" ref="ak" />
  </div>
</template>

<script>
import AddOrUpdate from "./add-or-update";
import Ak from "./ak";
import { getList , deleteApp } from '@/api/exter/app'
export default {
  data() {
    return {
      dataForm: {
        name: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      akVisible: false
    };
  },
  components: {
    AddOrUpdate,
    Ak
  },
  activated() {
    this.dataForm.name = this.$route.query.name;

    let pageIndex = this.$route.query.pageIndex;
    if (pageIndex) {
      this.pageIndex = parseInt(pageIndex);
    }

    let pageSize = this.$route.query.pageSize;
    if (pageSize) {
      this.pageSize = parseInt(pageSize);
    }

    this.getDataList();
  },
  methods: {
    refresh() {
      this.$router.push({
        name: "exter_app",
        query: {
          name: this.dataForm.name,
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        }
      });
    },
    search() {
      this.pageIndex = 1;
      this.getDataList();
      this.refresh();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true; 
      getList({
          page: this.pageIndex,
          limit: this.pageSize,
          name: this.dataForm.name
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalCount = data.data.totalCount;
          if(data.data.page != this.pageIndex) {
            this.pageIndex = data.data.page
          }
        } else {
          this.dataList = [];
          this.totalCount = 0;
          this.pageIndex = 1
        }
        
      }).finally(()=>{
        this.dataListLoading = false
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
      this.refresh();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
      this.refresh();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    akUpdateHandle(id) {
      this.akVisible = true;
      this.$nextTick(() => {
        this.$refs.ak.init(id);
      });
    },
    // 删除
    deleteHandle(row) {
      var ids = row
        ? [row.id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      var names = row
        ? [row.name]
        : this.dataListSelections.map(item => {
            return item.name;
          });
      this.$confirm(`确定进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => { 
        deleteApp(ids).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500
            });
            this.getDataList();
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
